/*
 *  This file is part of "SchoolChoice",
 *  a school registration system.
 *
 *  Copyright (C) 2009 Nicolas Decerf, Olivier Jacquemin.
 *
 *  "SchoolChoice" is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  "SchoolChoice" is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program: see the file COPYING.txt.  If not, see
 *  http://www.gnu.org/licenses/.
 */

package be.schoolchoice.webapp.web;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.http.*;
import org.jabsorb.JSONRPCBridge;

import be.schoolchoice.webapp.data.PMF;
import be.schoolchoice.webapp.data.School;

@SuppressWarnings("serial")
public class InitServlet  extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws IOException {
		try {
			HttpSession session = req.getSession();
			JSONRPCBridge jsonBridge = (JSONRPCBridge) session.getAttribute("JSONRPCBridge");
			if(jsonBridge == null)
			{
				jsonBridge = new JSONRPCBridge();
				
				jsonBridge.registerClass("AssignmentService", AssignmentService.class);
				jsonBridge.registerClass("SchoolsService", SchoolsService.class);
				jsonBridge.registerClass("UserService", UserService.class);
				
				session.setAttribute("JSONRPCBridge", jsonBridge);
				
				// TODO: remove code below, which deletes all objects from the datastore.
		        PersistenceManager pm = PMF.get().getPersistenceManager();
		        String internalQuery = "select from " + School.class.getName();
		        List<School> schools = (List<School>) pm.newQuery(internalQuery).execute();
		        for (School school : schools) {
		        	pm.deletePersistent(school);
		        }
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
